import type { UploadFile } from 'element-plus'

export type fileBos = {
  /** 文件名 */
  fileName: String
  /** 文件路径 */
  filePath: String
  /** 文件大小 */
  fileSize?: Number
  /** 文件后缀 */
  fileSuffix?: String
}

/**
 * 获取文件后缀名
 * @param fileName
 * @returns
 */
export const getFileSuffix = (fileName: String) => {
  return fileName.split('.').pop()
}

/**
 * fileList格式转fileBos
 * @param fileList
 * @returns
 */
export const fileListToFileBos = (fileList: Array<UploadFile>) => {
  const files = []
  fileList.forEach((item) => {
    files.push({
      fileName: item.name,
      filePath: item.url,
      fileSize: item.size,
      fileSuffix: getFileSuffix(item.name),
    })
  })
  return files
}

/**
 * fileBos格式转fileList
 * @param fileBos
 * @returns
 */
export const fileBosToFileList = (fileBos: Array<fileBos>) => {
  const files = []
  fileBos.forEach((item) => {
    files.push({
      name: item.fileName,
      url: item.filePath,
      size: item.fileSize,
    })
  })
  return files
}
